package com.repair.service.impl;

import com.repair.entity.User;
import com.repair.entity.RepairOrder;
import com.repair.service.AdminService;
import com.repair.service.UserService;
import com.repair.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    private final UserService userService;
    private final RepairOrderService repairOrderService;

    @Autowired
    public AdminServiceImpl(UserService userService, RepairOrderService repairOrderService) {
        this.userService = userService;
        this.repairOrderService = repairOrderService;
    }

    @Override
    public List<User> getAllUsers() {
        return userService.list();
    }

    @Override
    @Transactional
    public void updateUserStatus(Long userId, Integer status) {
        userService.updateUserStatus(userId, status);
    }

    @Override
    @Transactional
    public void deleteUser(Long userId) {
        userService.delete(userId);
    }

    @Override
    public List<User> getAllRepairmen() {
        return userService.getRepairmen();
    }

    @Override
    @Transactional
    public void updateRepairmanStatus(Long repairmanId, Integer status) {
        userService.updateUserStatus(repairmanId, status);
    }

    @Override
    @Transactional
    public void deleteRepairman(Long repairmanId) {
        userService.delete(repairmanId);
    }

    @Override
    public List<RepairOrder> getAllOrders() {
        return repairOrderService.getRecentOrders();
    }

    @Override
    public List<RepairOrder> getOrdersByStatus(Integer status) {
        return repairOrderService.getOrdersByStatus(status);
    }

    @Override
    @Transactional
    public void updateOrderStatus(Long orderId, Integer status) {
        repairOrderService.updateOrderStatus(orderId, status);
    }

    @Override
    @Transactional
    public void deleteOrder(Long orderId) {
        repairOrderService.removeById(orderId);
    }

    @Override
    public Map<String, Object> getDashboardData() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("totalUsers", userService.count());
        stats.put("totalRepairmen", userService.countRepairmen());
        stats.put("totalOrders", repairOrderService.count());
        stats.put("todayOrders", repairOrderService.countTodayOrders());
        stats.put("pendingOrders", repairOrderService.countPendingOrders());
        return stats;
    }

    @Override
    public List<Map<String, Object>> getOrderStatistics() {
        return repairOrderService.getOrderStatistics();
    }

    @Override
    public List<Map<String, Object>> getRepairmanStatistics() {
        return repairOrderService.getRepairmanStatistics();
    }
} 